home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 029a / greq.zip / GREQ.DOC < prev   
Text File  |  1991-12-31  |  14KB  |  507 lines

  1.                                 page 65,132
  2.  
  3. ;GREQ.COM A very compact 'Stand Alone' file search routine
  4. ; with looping, case insensitivity.
  5. ;May also be used as utility search routine in other software
  6. ;
  7. ;12/31/91 Ted Dowling, WJ2T
  8. ;
  9. ;*********************************************
  10. GREQ segment     ;define code segment
  11.     ASSUME    CS:GREQ
  12. ;---------------------------------------------
  13.  
  14.     MAIN    PROC    FAR    ;main part of program
  15.  
  16.     nambuf1        equ    80h    ;address of read file
  17.     wkb_siz        equ    80    ;half size of wkb
  18.         org 100h
  19.  
  20. start:          ;starting execution address
  21.  
  22. ;set up stack for return
  23.         push    ds      ;save old data segment
  24.         sub     ax,ax   ;put zero in AX
  25.         push    ax      ;save it on stack
  26.  
  27. ;---------------------------------------------
  28. ;---------------------------------------------
  29.  
  30.     mov    bx,offset nambuf1    ;filename location
  31.     call    get_file    ; test and prepare for handle assign
  32.     mov    dx,offset nambuf1+2    ;now 
  33.     mov    bx,offset handle1    ; open the file
  34.     call    open_file    ;and store the handle
  35.     call    cls        ;clear screen
  36. nxt_req:mov    dx,offset greq_req    ;send request message
  37.     call    print_str    ;local screen display
  38.     mov    dx,offset key_buf    ;search arg store
  39.     mov    di,dx    
  40.     call    key    ;buffered kbd input
  41.     inc    dx    ;DX=actual count
  42.     mov    al,[di+1]    ;check count
  43.     cmp    al,0    ;no input?
  44.     jz    greqend    ;no, done
  45.     call    find_req    ; search and display
  46.     jmp    nxt_req
  47. greqend:
  48.     mov    bx,offset handle1    ;pointer in BX
  49.     call    close_file
  50.     ret
  51.  
  52.     MAIN    ENDP    ;end main code 
  53.  
  54. ;*********************************************
  55. ;---------------------------------------------
  56.     PUBLIC    FIND_REQ
  57. ;Sets file pointer to beginning of file then
  58. ;searches for argument string while placing bytes
  59. ;read in circular buffer.  When string is found,
  60. ;continues to load buffer until occurrence of next
  61. ;CR
  62.     FIND_REQ    PROC    NEAR
  63.  
  64.     call    point_z    ;zero the file pointer
  65.     mov    al,1    ;now set
  66.     mov    di,offset status    ;status byte
  67.     mov    [di],al    ;reset all status
  68.  
  69. wkld0:    mov    dx,offset wkb0    ;circular buffer area #1
  70.     mov    ah,1    ;set status
  71.     mov    di,offset status
  72.     xor    [di],ah
  73.     call    read_file    ;load wkb0
  74.     cmp    ax,0    ;is EOF
  75.     jnz    go1    ; no, continue
  76.     jmp    dun_find
  77.  
  78. wkld1:    mov    dx,offset wkb1    ;circular buffer area #2
  79.     mov    ah,1    ;set status
  80.     mov    di,offset status
  81.     xor    [di],ah
  82.     call    read_file    ;load wkb1
  83.     cmp    ax,0    ;is EOF
  84.     jnz    go1    ; no, continue
  85.     jmp    dun_find
  86.  
  87. go1:    mov    di,offset status    ;check for
  88.     mov    al,[di]    ; incomplete read
  89.     mov    di,dx    ;get wkb pointer start
  90.     test    al,2    ;is bit 2 set?
  91.     jz    nomch1    ; no
  92.     mov    di,offset status    ;reset
  93.     xor    byte ptr [di],2    ;the incomplete
  94.     mov    di,dx    ;wkb pointer start
  95.     jmp    fwd_pt1    ; yes, continue printing
  96.  
  97. nomch1:    test    al,4    ; incomplete test
  98.     jz    nomch2    ; no
  99.     mov    di,offset status    ;reset
  100.     xor    byte ptr [di],4    ;the incomplete
  101.     mov    di,dx    ;wkb pointer start
  102.     dec    di    ;adj for inc
  103.     jmp    mach3    ; yes, continue testing
  104.  
  105. nomch2:    test    al,8    ;incomplete CR search fwd
  106.     jz    nomch3    ;no
  107.     mov    di,offset status    ;reset
  108.     xor    byte ptr [di],8    ;the incomplete CR
  109.     mov    di,dx    ;wkb pointer start
  110.     mov    al,0dh    ;reset al
  111.     jmp    test_cr
  112.  
  113. nomch3:    mov    di,dx    ;pointer start
  114.     dec    di    ; start down 1
  115. mach2:    mov    si,offset key_buf+2    ;past counts
  116. mach3:    mov    al,[si]    ;get byte
  117.     inc    di    ;bump pointer
  118.     cmp    al,0dh    ;is end of argument?
  119.     jz    test_cr    ;yup, got one
  120.     mov    ah,al    ;set other case
  121.     cmp    ah,5ah    ;upper case 'Z'
  122.     jle    case1    ;yes, or non alpha
  123.     cmp    ah,61h    ;lower case 'a'
  124.     jl    case    ; go, non-alpha
  125.     cmp    ah,7ah    ;lower case 'z'
  126.     jg    case    ; go, non-alpha
  127.     sub    ah,20h    ;change to upper case
  128.     jmp    case    ; and go
  129. case1:    cmp    ah,41h    ;upper case 'A'
  130.     jl    case    ; go, non-alpha
  131.     add    ah,20h    ;change to lower case
  132. case:    cmp    al,[di] ; is byte a match?
  133.     jz    mach1    ; yes 
  134.     cmp    ah,[di]    ;check other case
  135.     jz    mach1    ; yes
  136.     mov    ah,1ah    ;EOF byte
  137.     cmp    ah,[di]    ;check EOF
  138.     jnz    mach5
  139.     jmp    dun_find    ;search req finished
  140. mach5:    loop    mach2    ; no check next
  141.  
  142. ;if loop falls through at this point we have run out 
  143. ;of data with no match and must switch and load other wkb
  144.     jmp    reload
  145.  
  146. ;SI has pointed to CR at the end of argument meaning that
  147. ;total arg has been processed.  We must now ensure that there
  148. ;is a CR following
  149.  
  150. test_cr:
  151.     cmp    al,[di]    ;is CR?
  152.     jz    bakup    ;yup, go to it
  153.     inc    di    ; no, bump
  154.     loop    test_cr
  155.     mov    bx,offset status    ;need reload
  156.     mov    ah,8    ;creload bit
  157.     or    [bx],ah    ;in status
  158.  
  159. reload:    mov    bx,offset status
  160.     mov    ah,[bx]
  161.     test    ah,1    ;active wkb=1?
  162.     jz    rlod1    ; for long jump
  163.     jmp    wkld0    ;yes, now load wkb0
  164. rlod1:    jmp    wkld1    ; yes, load wkb1
  165.  
  166. mach1:    inc    si    ;check next too!
  167.     loop    mach3
  168.  
  169. ;if loop falls through at this point we have run out
  170. ;of matching data in wkb and must load and continue test
  171. ;in other wkb
  172.  
  173.     mov    bx,offset status    ;status bit
  174.     mov    al,4    ;#4 is incomplete test
  175.     or    [bx],al    ; test indicator
  176.     jmp    reload
  177.  
  178. bakup:
  179. ;we have a complete match and have located forward CR
  180. ; and will now need to back up the DI pointer and find
  181. ; the last occurrence of a CR
  182.     push    cx    ;save count and
  183.     push    di    ;pointer at last byte
  184. bakup1:    dec    di    ;de_bump
  185.     mov    al,[di]    ;and check
  186.     cmp    al,0ffh    ;out of buffer wkb0?
  187.     jnz    bakup2    ;no, continue read
  188.     mov    di,offset wkbe-1    ;position DI to end wkb2
  189.     mov    al,[di]    ; and check first byte
  190. bakup2:    cmp    al,0dh    ;is CR?
  191.     jnz    bakup1    ;no, continue back
  192.     inc    di    ; yes, now pass
  193.     mov    al,[di]    ; CR and check
  194.     cmp    al,0ffh    ;buffer end?
  195.     jnz    bakup3    ; no, continue
  196.     mov    di,offset wkb0    ;yes, loop to other wkb
  197.     mov    al,[di]    ; and check first byte
  198. bakup3:    cmp    al,0ah    ; for linefeed
  199.     jnz    fwd_pt1    ;no, begin printing
  200. fwd_pt:    inc    di
  201. fwd_pt1:mov     dl,[di]       ;put chr in dl
  202.     cmp    dl,0ffh    ;is end of buffer?
  203.     jz    fwd_pt2    ; yes, need more data
  204.         mov     ah,2        ;display output function
  205.         int     21h         ;call DOS
  206.     cmp    dl,0dh    ;is end line?
  207.     jnz    fwd_pt    ; no, get next char
  208.     mov    dl,0ah    ; add LF to CR
  209.         mov     ah,2        ;display output function
  210.         int     21h         ;call DOS
  211.     pop    di    ;restore end
  212.     pop    cx    ; of print
  213.     jmp    mach5    ;end of this print
  214.  
  215. fwd_pt2:    
  216. ;at this point we have run to the end of the buffer and
  217. ;have previously reloaded other buffer while searching
  218. ;for forward CR. Must now set DI to currently loaded
  219. ;buffer and cx to full wkb_siz
  220.  
  221.     mov    bx,offset status
  222.     mov    al,1    ;check for buffer 1 active
  223.     test    [bx],al    ; if true then NZ
  224.     jnz    buffer1    ;wkb1 was last loaded
  225.     mov    di,offset wkb0    ;wkb0 was last loaded
  226.     jmp    fwd_pt1
  227. buffer1:mov    di,offset wkb1
  228.     jmp    fwd_pt1    ;and resume
  229.  
  230. dun_find:    ret
  231.  
  232.     FIND_REQ    ENDP
  233. ;*********************************************
  234. ;---------------------------------------------
  235.     BUFFERS    PROC    NEAR
  236. handle1:dw    ?
  237.     db    0ffh    ;end stop wkb0
  238.  
  239. status:    db    0    ; bit1=wkb#,bit2=incomplete read
  240.             ; bit3=incomplete test
  241.             ; bit4=incomplete CR search
  242.             ; bit5=match in progress
  243.  
  244.     db    0ffh    ;wkb delimiter at start
  245. wkb0    db    wkb_siz dup(0)
  246. wkb1    db    wkb_siz dup(0)
  247. wkbe    db    0ffh    ;wkb delimiter at end
  248. key_buf    db    80,0,10 dup('key_bufr')
  249. greq_req:db    0dh,0ah,0ah,'Enter Search Argument For Data Desired'
  250.     db    0dh,0ah,'Or press [ENTER] to terminate routine'
  251.     db    0dh,0ah,'> ','$'
  252.     BUFFERS    ENDP
  253. ;*********************************************
  254. ;---------------------------------------------
  255.     PUBLIC    READ_FILE
  256. ;reads the datafile. Enter with DX=buffer ptr
  257.  
  258.     READ_FILE    PROC    NEAR
  259.  
  260.     mov    cx,wkb_siz    ;read file size
  261.     mov    bx,offset handle1    ;pointer in BX
  262.     mov    bx,[bx]    ;handle in BX
  263.         mov     ah,3fh      ;read file function
  264.         int     21h         ;call DOS
  265.         jnc     go2         ;error return?
  266.         jmp     error       ;yes, terminate pgm
  267. go2:    cmp    ax,0    ;EOF?
  268.     jz    endfile
  269.     ret
  270. endfile:pop    dx    ;pseudo SP correct
  271.     ret
  272.     READ_FILE    ENDP
  273. ;*********************************************
  274. ;---------------------------------------------
  275.     PUBLIC    KEY
  276. ;get buffer size DS:DX, get keyboard to CR, leave
  277. ; string length DS:DX+1
  278.  
  279.     KEY    PROC    NEAR
  280.  
  281.         mov     ah,0ah      ;buffered keyboard
  282.         int     21h         ;call DOS
  283.         ret
  284.  
  285.     KEY    ENDP
  286. ;*********************************************
  287. ;---------------------------------------------
  288.     PUBLIC    GET_FILE
  289. ;gets file name of file to be read from entry
  290. ; and sets it up for handle assignment
  291. ; enter with BX pointing to fcb filename
  292.  
  293.     GET_FILE    PROC    NEAR
  294.  
  295.         mov     al,[bx]  ; check to see if
  296.         cmp     al,0        ; filename installed
  297.         jnz     newfile     ;yes, continue
  298.         mov     dx,offset nofile ;no file message
  299.         mov     ah,9h       ;print message funct
  300.         int     21h         ;call DOS
  301.         jmp     exit     ;and exit
  302. ;ADJUST PATHNAME OF FILE TO BE OPENED
  303. newfile:
  304.         mov     bx,offset nambuf1      ;get # bytes read
  305.         mov     bl,[bx]
  306.         mov     bh,0        ; into BX
  307.         mov     [byte ptr nambuf1+bx+1],0    ;zero into byte
  308.  
  309. done:    ret
  310.  
  311.     GET_FILE    ENDP
  312. ;*********************************************
  313. ;---------------------------------------------
  314.     PUBLIC    PRINT_STR
  315. ;Enter with DX pointing to string offset
  316. ;String ends with '$'
  317.     PRINT_STR    PROC    NEAR
  318.  
  319.     mov    ah,09h    ;print string function
  320.     int    21h    ;call DOS
  321.     ret
  322.  
  323.     PRINT_STR    ENDP
  324.  
  325. ;*********************************************
  326. ;---------------------------------------------
  327.     PUBLIC    ERROR
  328.     ERROR    PROC    NEAR
  329. ;error message generator using binihex converter
  330.  
  331.     mov     dx,offset emess ;error message
  332.         mov     ah,9        ;print message function
  333.         int     21h         ;call DOS
  334.         call    binihex     ;print error number
  335.  
  336.         jmp     exit
  337.  
  338.     ERROR    ENDP
  339. ;*********************************************
  340. ;---------------------------------------------
  341.     PUBLIC    EXIT
  342.     EXIT    PROC    NEAR
  343.  
  344.     pop    dx    ;Pseudo SP correction
  345.     jmp    greqend
  346.  
  347.     EXIT    ENDP
  348. ;*********************************************
  349. ;---------------------------------------------
  350.     PUBLIC    BINIHEX
  351.     BINIHEX    PROC    NEAR
  352.   
  353.         mov     ch,4
  354. rotate: mov     cl,4        ;set count to 4 bits
  355.         rol     bx,cl       ;left digit to right
  356.         mov     al,bl       ;move to al
  357.         and     al,0fh      ;mask off left digit
  358.         add     al,30h      ;convert to ascii
  359.         cmp     al,3ah      ;is it >9?
  360.         jl      printit     ;jump if digit =0-9
  361.         add     al,7h       ;digit=a to f
  362.  
  363.  printit:
  364.  
  365.         mov     dl,al       ;put ascii chr in dl
  366.         mov     ah,2        ;display output function
  367.         int     21h         ;call DOS
  368.         dec     ch          ;done 4 digits?
  369.         jnz     rotate      ;not yet
  370.         ret
  371.  
  372.     BINIHEX    ENDP
  373. ;*********************************************
  374. ;---------------------------------------------
  375.     PUBLIC    OPEN_FILE
  376.     OPEN_FILE    PROC    NEAR
  377. ;opens file and stores handle
  378. ;enter with DX=address of filename; BX=handle storage buffer
  379.  
  380.         mov     al,0        ;file open for reading
  381.         mov     ah,3dh      ;open file function
  382.         int     21h         ;call DOS
  383.         mov     [bx],ax   ; and store it
  384.         jc      error       ;error return?
  385.     ret
  386.  
  387.     OPEN_FILE    ENDP
  388.  
  389. ;*********************************************
  390. ;---------------------------------------------
  391.     PUBLIC    POINT_Z
  392.  
  393.     ;positions pointer to the beginning of the
  394.         ;file specified by handle in BX
  395.  
  396.     POINT_Z    PROC    NEAR
  397.  
  398.     push    cx    ;save registers
  399.     push    dx    ;used
  400.     push    bx
  401.     mov    bx,offset handle1
  402.     xor    cx,cx    ;zero file
  403.     mov    dx,cx    ;vectors
  404.     call    point_x    ; and set pointer
  405.     pop    bx
  406.     pop    dx    ; restore
  407.     pop    cx    ;registers used
  408.     ret
  409.  
  410.     POINT_Z    ENDP
  411.  
  412. ;*********************************************
  413. ;---------------------------------------------
  414.     PUBLIC    POINT_X
  415.  
  416.     ;positions pointer to point specified by CX:DX
  417.         ;enter with BX=handle pointer
  418.  
  419.     POINT_X    PROC    NEAR
  420.  
  421.     mov    bx,[bx]    ;handle in BX
  422.         mov     al,0        ;absoloute address
  423.         mov     ah,42h      ;  for added new data
  424.         int     21h         ;and call DOS again
  425.     ret
  426.  
  427.     POINT_X    ENDP
  428.  
  429. ;*********************************************
  430. ;---------------------------------------------
  431.  
  432.     PUBLIC    CLOSE_FILE
  433.     CLOSE_FILE    PROC    NEAR
  434. ;closes file handle
  435. ;enter with BX=handle storage buffer
  436.  
  437.         mov     bx,[bx]        ;put handle in BX
  438.         mov     ah,3eh      ;close file function
  439.         int     21h         ;call DOS
  440.         jc      error       ;error return?
  441.     ret
  442.  
  443.     CLOSE_FILE    ENDP
  444.     
  445.     
  446. ;*********************************************
  447. ;---------------------------------------------
  448.  
  449.     PUBLIC    PRINT_MEM
  450. ;reads and screen prints buffer contents
  451. ;enter with BX=memory start; CX=read length
  452.     PRINT_MEM    PROC    NEAR
  453.  
  454. ;DISPLAY BUFFER CONTENTS
  455.  
  456.     mov    dl,[bx]    ;get byte in AL
  457.     inc    bx    ;and bump pointer
  458.         cmp     dl,1ah      ;is it EOF
  459.     jz    endpr    ;yes, stop printing
  460.     mov    ah,02h    ;char output routine
  461.     int    21h    ;call DOS
  462.     cmp    dl,0dh    ;is CR
  463.     jnz    print_mem; no get next byte
  464. endpr:    ret
  465.  
  466.     PRINT_MEM    ENDP
  467.  
  468. ;*********************************************
  469. ;---------------------------------------------
  470.     PUBLIC    CLS
  471. ;Clears the local screen
  472.     CLS    PROC    NEAR
  473.  
  474.     mov    bh,6
  475.     mov    ah,0
  476.     mov    al,3
  477.     int    10h
  478.     sub    cx,cx
  479.     mov    dh,18h    ;80th row
  480.     mov    dl,4fh    ;24th column
  481.     xor    al,al    ;zero in AL
  482.     mov    bh,07h    ;color attribute
  483.     mov    ah,06    ;scroll up function
  484.     int    10h    ;BIOS call
  485.     ret
  486.  
  487.     CLS    ENDP
  488. ;*********************************************
  489. ;---------------------------------------------
  490.  
  491. nofile  db      13,10,10,'GREQ.COM',13,10,10
  492.     db    'Ted Dowling, WJ2T, 12/31/91',13,10,10
  493.         db      13,10,'For use locating strings in a ASCII'
  494.         db      13,10,'file and displaying all occurrences'
  495.         db      13,10,13,10,'Line length of file must not exceed'
  496.         db      13,10,'screen width between carriage returns'
  497.         db      13,10,'use=GREQ [INFILE.EXT]'
  498.         db      13,10,10,' $'
  499.  
  500. emess    db    'Error $'
  501. ;*********************************************
  502.  
  503.     GREQ        ENDS
  504.  
  505.         end     start    ;end assembly
  506.  
  507.